package jsyf.oa.service.report.visitCustomer;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.report.attendance.AttendanceDao;
import jsyf.oa.dao.report.visitCustomer.VisitCustomerDao;
import jsyf.oa.dao.system.CheckDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.facade.report.visitCustomer.VisitCustomerFacade;
import jsyf.oa.service.report.reportCommon.ReportCommonMethodsAndConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

/**
 * Created by jjx on 2018/4/5.
 */
@SuppressWarnings("all")
@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class VisitCustomerService implements VisitCustomerFacade {

    @Autowired
    private VisitCustomerDao visitCustomerDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AttendanceDao attendanceDao;
    @Autowired
    private ReportCommonMethodsAndConstant common;
    @Autowired
    private CheckDao checkDao;

    /**
     * @dateTime: 时间  [2018-04-06]--今天 || [2018-04/2018-03]--本月，其他月 ||  [2018-04-05-w]--本周(12个字符 -w 可任意)
     * @userId: 用户ID
     * @type: 0-电访 1-面访
     */

    @Override
    public JSONObject getPeopleByUserId(String jsonData) {
        JSONObject results = new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String time = jsonParams.getString("dateTime");
        String today = jsonParams.getString("today");
        String type = jsonParams.getString("type");
        Integer spacing = null;
        if (time.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(time);
        }
        List<JSONObject> faceTargetDay = null;
        List<JSONObject> telTargetDay = null;
        JSONObject faceTargetInfo = null;
        JSONObject telTargetInfo = null;
        if (type.equals("1")) {
            faceTargetDay = visitCustomerDao.getPeopleByUserId(userId, time, today, common.NUMBER_ONE, spacing);
            faceTargetInfo = visitCustomerDao.getPeopleInfoByUserId(userId, time, common.NUMBER_ONE, spacing);
            //判断是否为第一天，无实际意义。
            String panduanDate = "YYYY-MM-DD";
            for (JSONObject jsonObject : faceTargetDay) {
                jsonObject.put("distance", "");
                if (!StringUtils.isBlank(jsonObject.getString("latitude")) && !StringUtils.isBlank(jsonObject.getString("longitude"))
                        && !StringUtils.isBlank(jsonObject.getString("arrivalsLatitude")) && !StringUtils.isBlank(jsonObject.getString("arrivalsLongitude"))) {
                    double lat1 = Double.parseDouble(jsonObject.getString("latitude"));
                    double lng1 = Double.parseDouble(jsonObject.getString("longitude"));
                    double lat2 = Double.parseDouble(jsonObject.getString("arrivalsLatitude"));
                    double lng2 = Double.parseDouble(jsonObject.getString("arrivalsLongitude"));
                    jsonObject.put("distance", common.getDistanceByLatAndLng(lat1, lng1, lat2, lng2));
                }
                jsonObject.remove("latitude");
                jsonObject.remove("longitude");
                jsonObject.remove("arrivalsLatitude");
                jsonObject.remove("arrivalsLongitude");
                String date = jsonObject.getString("date");
                if (!date.equals(panduanDate)) {
                    panduanDate = date;
                    jsonObject.put("newDay", "1");
                    //获取考勤->签到和请假为1
                    Integer singAndLeave = attendanceDao.getPageCount(userId, date);
                    if (singAndLeave == 1) {
                        //获取考勤->请假为1
                        Integer leave = attendanceDao.getLeaveCount(userId, date);
                        if (leave == 1) {
                            jsonObject.put("singState", "请假");
                        } else {
                            jsonObject.put("singState", "签到");
                        }
                    } else {
                        jsonObject.put("singState", "缺勤");
                    }
                }
            }
            results.put("faceTargetInfo", faceTargetInfo);
            results.put("faceTargetDay", faceTargetDay);
        } else {
            //time = common.getYesterDayForTelTarget(time);
            telTargetDay = visitCustomerDao.getPeopleByUserId(userId, time, today, common.NUMBER_ZERO, spacing);
            telTargetInfo = visitCustomerDao.getPeopleInfoByUserId(userId, time, common.NUMBER_ZERO, spacing);
            results.put("telTargetDay", telTargetDay);
            results.put("telTargeInfo", telTargetInfo);
        }
        return results;
    }

    @Override
    public JSONObject getRegionInfo(String jsonData) {
        JSONObject results = new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String dateTime = jsonParams.getString("dateTime");
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        JSONObject faceInfo = visitCustomerDao.getByFaceInfo(dateTime, todayTime, spacing, userId);
        JSONObject telInfo = visitCustomerDao.getByTelInfo(dateTime, todayTime, spacing, userId);
        results.put("faceInfo", faceInfo);
        results.put("telInfo", telInfo);
        return results;
    }

    @Override
    public List<JSONObject> getForPersonalByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        String type = jsonParams.getString("type");
        String target = "";
        Integer spacing = null;
        if (String.valueOf(common.NUMBER_ONE).equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
            //dateTime = common.getYesterDayForTelTarget(dateTime);
        }
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        String firstTime = DateUtils.getFirstDayOfMonth();
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        //列表
        return visitCustomerDao.getForPersonalByUserId(target, Integer.valueOf(type), dateTime, spacing, userId, firstTime, todayTime);
    }

    @Override
    public List<JSONObject> getForDepart(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = null;
        String userId = null;
        if (jsonParams.containsKey("companyId")) {
            companyId = jsonParams.getString("companyId");
        }
        String dateTime = jsonParams.getString("dateTime");
        if (jsonParams.containsKey("userId")) {
            userId = jsonParams.getString("userId");
        }
        String type = jsonParams.getString("type");
        String target = "";
        Integer spacing = null;
        if (String.valueOf(common.NUMBER_ONE).equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
            //dateTime = common.getYesterDayForTelTarget(dateTime);
        }
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        StringBuilder condition = new StringBuilder();
        condition.append("  (SELECT companyId FROM v_sys_user_for_report WHERE userid in ('" + userId + "'))");
        //列表
        return visitCustomerDao.getForDepart(target, Integer.valueOf(type), dateTime, spacing, userId, todayTime, companyId);
    }

    @Override
    public List<JSONObject> getForGroupByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        String type = jsonParams.getString("type");
        JSONObject user = userDao.getViewJSONById(userId);
        String target = "";
        Integer spacing = null;
        if (String.valueOf(common.NUMBER_ONE).equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
            //dateTime = common.getYesterDayForTelTarget(dateTime);
        }
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        String companysId = common.getCompanysIdExceptDKHByUserId(userId);

        List<JSONObject> users = userDao.getViewJSONListByCondition(" AND id=? GROUP BY companyId", new Object[]{userId});
        StringBuffer ids = new StringBuffer();
        String companyIds = "";
        for (JSONObject object : users) {
            ids.append("'");
            ids.append(object.getString("companyId"));
            ids.append("'");
            ids.append(",");
        }
        companyIds = ids.toString().substring(0, ids.toString().length() - 1);
        String condition = "WHERE companyId in (" + companyIds + ") AND companyName not LIKE '%集团%'";
        if ("1".equals(user.getString("manageFlag"))) {
            condition = " WHERE companyName not LIKE '%集团%'";
        }
        //列表
        return visitCustomerDao.getForGroupByUserId(target, Integer.valueOf(type), dateTime, spacing, userId, todayTime, companyIds, condition);
    }

    @Override
    public List<JSONObject> getAllManagerByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String type = jsonParams.getString("type");
        String target = "";
        Integer spacing = null;
        if (String.valueOf(common.NUMBER_ONE).equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
            //dateTime = common.getYesterDayForTelTarget(dateTime);
        }
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        //列表
        return visitCustomerDao.getAllManagerByCompanyId(target, Integer.valueOf(type), dateTime, spacing, todayTime, companyId);
    }

    @Override
    public List<JSONObject> getRegionByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        return visitCustomerDao.getRegionByUserId(userId);
    }

    @Override
    public List<JSONObject> getManagerByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = null;
        String userId = null;
        if (jsonParams.containsKey("companyId")) {
            companyId = jsonParams.getString("companyId");
        }
        if (jsonParams.containsKey("userId")) {
            userId = jsonParams.getString("userId");
        }
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        return visitCustomerDao.getManagerByUserId(dateTime, spacing, todayTime, userId, companyId);
    }

    @Override
    public List<JSONObject> getCustomerByManage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String entourage = jsonParams.getString("entourage");
        String dateTime = jsonParams.getString("dateTime");
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        List<JSONObject> customer = visitCustomerDao.getCustomerByManage(dateTime, userId, entourage, todayTime, spacing);
        //根据入职时间计算出入职天数
        List<JSONObject> listSendData = common.getEntryDate(customer);
        return listSendData;
    }

    @Override
    public JSONObject getCustomerVisitById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        return visitCustomerDao.getCustomerVisitById(jsonParams.getString("id"));
    }

    //区域经理陪跑
    @Override
    public List<JSONObject> getCompanyByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        String companysId = common.getCompanysIdExceptDKHByUserId(userId);
        //列表
        return visitCustomerDao.getCompanyByUserId(dateTime, spacing, todayTime, companysId);
    }

    @Override
    public List<JSONObject> getForBigCustomerDepart(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = null;
        String userId = null;
        if (jsonParams.containsKey("companyId")) {
            companyId = jsonParams.getString("companyId");
        }
        String dateTime = jsonParams.getString("dateTime");
        if (jsonParams.containsKey("userId")) {
            userId = jsonParams.getString("userId");
        }
        String type = jsonParams.getString("type");
        String target = "";
        Integer spacing = null;
        if (String.valueOf(common.NUMBER_ONE).equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
            //dateTime = common.getYesterDayForTelTarget(dateTime);
        }
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        //列表
        return visitCustomerDao.getForBigCustomerDepart(target, Integer.valueOf(type), dateTime, spacing, userId, todayTime, companyId);
    }

    @Override
    public List<JSONObject> getBigCustomerForGroupByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        String type = jsonParams.getString("type");
        String target = "";
        Integer spacing = null;
        if (String.valueOf(common.NUMBER_ONE).equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
            //dateTime = common.getYesterDayForTelTarget(dateTime);
        }
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        String companysId = common.getDKHCompanysIdByUserId(userId);
        //列表
        return visitCustomerDao.getBigCustomerForGroupByUserId(target, Integer.valueOf(type), dateTime, spacing, userId, todayTime, companysId);
    }

    @Override
    public List<JSONObject> getAllBigCustomerManagerByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String type = jsonParams.getString("type");
        String target = "";
        Integer spacing = null;
        if (String.valueOf(common.NUMBER_ONE).equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
            //dateTime = common.getYesterDayForTelTarget(dateTime);
        }
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        //获取今日的日期
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        //列表
        return visitCustomerDao.getAllBigCustomerManagerByCompanyId(target, Integer.valueOf(type), dateTime, spacing, todayTime, companyId);
    }

    @Override
    public List<JSONObject> getForPersonalDayByUserIdAndDateTime(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Calendar c = Calendar.getInstance();
        String dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        if (jsonParams.containsKey("dateTime")) {
            dateTime = jsonParams.getString("dateTime");
        }
        String userId = jsonParams.getString("userId");
        String type = String.valueOf(common.NUMBER_ONE);
        if (jsonParams.containsKey("type")) {
            type = jsonParams.getString("type");
        }
        JSONObject userView = userDao.getViewJSONById(userId);
        String frontFlag = userView.getString("frontFlag");
        String areaType = userView.getString("areaType");
        List<JSONObject> jsonObjects = visitCustomerDao.getForPersonalDayByUserIdAndDateTime(Integer.valueOf(type), dateTime, userId);
        for (JSONObject jsonObject : jsonObjects) {
            jsonObject.put("distance", "");
            jsonObject.put("speed", "");
            jsonObject.put("frontFlag", frontFlag);
            jsonObject.put("areaType", areaType);
            if (!jsonObject.getString("longitude").isEmpty()
                    && !jsonObject.getString("latitude").isEmpty()
                    && !jsonObject.getString("arrivalsLatitude").isEmpty()
                    && !jsonObject.getString("arrivalsLongitude").isEmpty()) {
                double lat1 = Double.parseDouble(jsonObject.getString("latitude"));
                double lng1 = Double.parseDouble(jsonObject.getString("longitude"));
                double lat2 = Double.parseDouble(jsonObject.getString("arrivalsLatitude"));
                double lng2 = Double.parseDouble(jsonObject.getString("arrivalsLongitude"));
                String distance = common.getDistanceByLatAndLng(lat1, lng1, lat2, lng2);
                BigDecimal shijian = new BigDecimal(jsonObject.getString("hours"));
                //排除时间过短导致的时间为0的情况
                if (shijian.compareTo(new BigDecimal("0.0")) == 0) {
                    shijian = new BigDecimal("0.01");
                }//s路程,t时间,v速度  -->  v=s/t
                if ("1".equals(frontFlag) && "0".equals(areaType)) {
                    if (shijian.compareTo(new BigDecimal(1.50)) > 0) {
                        jsonObject.put("remind", 1);
                    } else {
                        jsonObject.put("remind", 0);
                    }
                }
                //不要问什么变量叫做s路程,t时间,v速度  -->  v=s/t
                BigDecimal lucheng;//s路程,t时间,v速度  -->  v=s/t
                if (distance.indexOf("公里") != -1) {
                    lucheng = new BigDecimal(distance.substring(0, distance.indexOf("公里")));
                } else {
                    lucheng = (new BigDecimal(distance.substring(0, distance.indexOf("米")))).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP);
                    distance = lucheng + "公里";
                }
                jsonObject.put("distance", distance);
                BigDecimal sudu = lucheng.divide(shijian, 2, BigDecimal.ROUND_HALF_UP);//s路程,t时间,v速度  -->  v=s/t
                jsonObject.put("speed", sudu + "公里/小时");
            }
            jsonObject.remove("latitude");
            jsonObject.remove("longitude");
            jsonObject.remove("arrivalsLatitude");
            jsonObject.remove("arrivalsLongitude");
        }
        return jsonObjects;
    }

    //问题标记小红点  ->  1:有问题; 0:没问题
    public JSONObject getForPersonalDayByUserIdAndDateTime(String userId, String dateTime) {
        List<JSONObject> result = visitCustomerDao.getForPersonalDayByUserIdAndDateTime(common.NUMBER_ONE, dateTime, userId);
        JSONObject userView = userDao.getViewJSONById(userId);
        String frontFlag = userView.getString("frontFlag");
        String areaType = userView.getString("areaType");
        JSONObject jsonObjects = new JSONObject();
        if (result != null && result.size() > 0) {
            jsonObjects = result.get(0);
            if (!StringUtils.isBlank(jsonObjects.getString("hours"))) {
                BigDecimal shijian = new BigDecimal(jsonObjects.getString("hours"));//s路程,t时间,v速度  -->  v=s/t
                if ("1".equals(frontFlag) && "0".equals(areaType)) {
                    //每日首次面访时间间隔是否在1.5h之内
                    if (shijian.compareTo(new BigDecimal(1.50)) > 0) {
                        jsonObjects.put("remind", 1);
                    } else {
                        jsonObjects.put("remind", 0);
                    }
                }
            } else {
                jsonObjects.put("remind", 1);
            }
        } else {
            jsonObjects.put("remind", 0);
        }
        return jsonObjects;
    }

    //判断哪些大区是可以被点击的!
    @Override
    public List<JSONObject> getForDepartByUserIdForClick(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        return visitCustomerDao.getForDepartByUserIdForClick(userId);
    }

    @Override
    public JSONObject getFirstVisitForPC(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String time = jsonParams.getString("dateTime");
        Integer spacing = null;
        if (time.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(time);
        }
        List<JSONObject> faceTargetDay = null;
        faceTargetDay = visitCustomerDao.getFirstVisitForPC(userId, time, spacing);
        //判断是否为第一天，无实际意义。
        String panduanDate = "YYYY-MM-DD";
        for (JSONObject jsonObject : faceTargetDay) {
            jsonObject.put("distance", "");
            if (!StringUtils.isBlank(jsonObject.getString("latitude")) && !StringUtils.isBlank(jsonObject.getString("longitude"))
                    && !StringUtils.isBlank(jsonObject.getString("arrivalsLatitude")) && !StringUtils.isBlank(jsonObject.getString("arrivalsLongitude"))) {
                double lat1 = Double.parseDouble(jsonObject.getString("latitude"));
                double lng1 = Double.parseDouble(jsonObject.getString("longitude"));
                double lat2 = Double.parseDouble(jsonObject.getString("arrivalsLatitude"));
                double lng2 = Double.parseDouble(jsonObject.getString("arrivalsLongitude"));
                jsonObject.put("distance", common.getDistanceByLatAndLng(lat1, lng1, lat2, lng2));
            }
            jsonObject.remove("latitude");
            jsonObject.remove("longitude");
            jsonObject.remove("arrivalsLatitude");
            jsonObject.remove("arrivalsLongitude");
            String date = jsonObject.getString("date");
            if (!date.equals(panduanDate)) {
                panduanDate = date;
                jsonObject.put("newDay", "1");
                //获取考勤->签到和请假为1
                Integer singAndLeave = attendanceDao.getPageCount(userId, date);
                if (singAndLeave == 1) {
                    //获取考勤->请假为1
                    Integer leave = attendanceDao.getLeaveCount(userId, date);
                    if (leave == 1) {
                        jsonObject.put("singState", "请假");
                    } else {
                        jsonObject.put("singState", "签到");
                    }
                } else {
                    jsonObject.put("singState", "缺勤");
                }
            }
        }
        JSONObject results = new JSONObject();
        results.put("faceVisitToday", faceTargetDay);
        return results;
    }

    @Override
    public JSONObject getMonthVisitForPC(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String time = jsonParams.getString("dateTime");
        if (time.length() == common.NUMBER_TWELVE) {
            time.substring(0, time.length() - 4);
        }
        List<JSONObject> jsonObjectList = visitCustomerDao.getMonthVisitForPC(userId, time);
        JSONObject results = new JSONObject();
        results.put("monthVisit", jsonObjectList);
        return results;
    }

    @Override
    public JSONObject stratAndEndFaceVisit(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String time = jsonParams.getString("dateTime");
        String userIds = checkDao.getChilderByUserId(userId, true);
        String qyjlUserIds = "";
        List<JSONObject> qyjlList = userDao.getUserByUserIdAndRoleName(userIds, "区域经理", userId);
        if (qyjlList != null && qyjlList.size() > 0) {
            for (int i = 0; i < qyjlList.size(); i++) {
                qyjlUserIds += "'" + qyjlList.get(i).getString("userId") + "',";
            }
        } else {
            qyjlUserIds = "'',";
        }
        qyjlUserIds = qyjlUserIds.substring(0, qyjlUserIds.length() - 1);
        //获取今日的日期
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        Integer spacing = null;
        if (time.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(time);
        }
        List<JSONObject> faceTargetDay = null;
        faceTargetDay = visitCustomerDao.stratAndEndFaceVisit(qyjlUserIds, time, spacing, todayTime);
        //判断是否为第一天，无实际意义。
        String panduanDate = "YYYY-MM-DD";
        for (JSONObject jsonObject : faceTargetDay) {
            jsonObject.put("signDistance", "");
            jsonObject.put("signOutDistance", "");
            if (!StringUtils.isBlank(jsonObject.getString("latitude")) && !StringUtils.isBlank(jsonObject.getString("longitude"))
                    && !StringUtils.isBlank(jsonObject.getString("arrivalsLatitude")) && !StringUtils.isBlank(jsonObject.getString("arrivalsLongitude"))) {
                double lat1 = Double.parseDouble(jsonObject.getString("latitude"));
                double lng1 = Double.parseDouble(jsonObject.getString("longitude"));
                double lat2 = Double.parseDouble(jsonObject.getString("arrivalsLatitude"));
                double lng2 = Double.parseDouble(jsonObject.getString("arrivalsLongitude"));
                jsonObject.put("signDistance", common.getDistanceByLatAndLng(lat1, lng1, lat2, lng2));
            }
            if (!StringUtils.isBlank(jsonObject.getString("signOutLongitude")) && !StringUtils.isBlank(jsonObject.getString("signOutLatitude"))
                    && !StringUtils.isBlank(jsonObject.getString("leaveLatitude")) && !StringUtils.isBlank(jsonObject.getString("leaveLongitude"))) {
                double lat1 = Double.parseDouble(jsonObject.getString("signOutLatitude"));
                double lng1 = Double.parseDouble(jsonObject.getString("signOutLongitude"));
                double lat2 = Double.parseDouble(jsonObject.getString("leaveLatitude"));
                double lng2 = Double.parseDouble(jsonObject.getString("leaveLongitude"));
                jsonObject.put("signOutDistance", common.getDistanceByLatAndLng(lat1, lng1, lat2, lng2));
            }
            jsonObject.remove("latitude");
            jsonObject.remove("longitude");
            jsonObject.remove("arrivalsLatitude");
            jsonObject.remove("arrivalsLongitude");

            jsonObject.remove("signOutLatitude");
            jsonObject.remove("signOutLongitude");
            jsonObject.remove("leaveLatitude");
            jsonObject.remove("leaveLongitude");
            /*String date = jsonObject.getString("reportDate");
            if (!date.equals(panduanDate)) {
                panduanDate = date;
                jsonObject.put("newDay", "1");
                //获取考勤->签到和请假为1
                Integer singAndLeave = attendanceDao.getPageCount(userId, date);
                if (singAndLeave == 1) {
                    //获取考勤->请假为1
                    Integer leave = attendanceDao.getLeaveCount(userId, date);
                    if (leave == 1) {
                        jsonObject.put("singState", "请假");
                    } else {
                        jsonObject.put("singState", "签到");
                    }
                } else {
                    jsonObject.put("singState", "缺勤");
                }
            }*/
        }
        JSONObject results = new JSONObject();
        results.put("faceVisitToday", faceTargetDay);
        return results;
    }

    @Override
    public List<JSONObject> getSignAndCustomerByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String dateTime = jsonParams.getString("dateTime");
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        return visitCustomerDao.getSignAndCustomerByUserId(userId, dateTime, spacing, todayTime);
    }

    @Override
    public List<JSONObject> getBigRegionManagerVisitForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        //获取今日的日期
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        //列表
        return visitCustomerDao.getBigRegionManagerVisitForGroup(dateTime, spacing, todayTime, userId);
    }

    @Override
    public List<JSONObject> getBigRegionManagerVisitForDepart(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        String visitType = jsonParams.getString("visitType");
        String companyId = jsonParams.getString("companyId");
        Integer userType = 0;
        if (jsonParams.containsKey("userType")) {
            userType = jsonParams.getInteger("userType");
        }
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        String yesdayTime = DateUtils.getYesterday();
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        return visitCustomerDao.getBigRegionManagerVisitForDepart(userId, dateTime, visitType, companyId, spacing, todayTime, yesdayTime, userType);
    }

    @Override
    public List<JSONObject> getBigRegionManagerPlanVisit(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        String visitType = jsonParams.getString("visitType");
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        //获取今日的日期
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        String yesdayTime = DateUtils.getYesterday();
        //列表
        return visitCustomerDao.getBigRegionManagerPlanVisit(userId, dateTime, spacing, todayTime, visitType, yesdayTime);
    }

    @Override
    public JSONObject getRMTodayVisit(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        String visitType = jsonParams.getString("visitType");
        //获取今日的日期
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        String yesterday = DateUtils.getYesterday();
        //列表
        List<JSONObject> list = visitCustomerDao.getRMTodayVisit(userId, dateTime, todayTime, visitType, yesterday);
        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;
    }

    @Override
    public JSONObject getBigRegionManagerFaceList(String jsonData) {
        JSONObject result = new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String userId = jsonParams.getString("userId");
        String time = jsonParams.getString("dateTime");
        String todayTime = jsonParams.getString("today");
        String visitType = jsonParams.getString("visitType");
        Integer spacing = null;
        if (dateTime.length() == common.NUMBER_TWELVE) {
            spacing = common.getWeekDay(dateTime);
        }
        String yesdayTime = DateUtils.getYesterday();
        List<JSONObject> faceDistance = null;
        List<JSONObject> telDistance = null;
        JSONObject faceInfo = null;
        JSONObject telInfo = null;
        if (visitType.equals("1")) {
            faceDistance = visitCustomerDao.getBigRegionManagerfaceList(userId, dateTime, spacing, todayTime, visitType);
            faceInfo = visitCustomerDao.getFaceTargetAndComplete(userId, dateTime, spacing, todayTime, visitType, yesdayTime);
            //判断是否为第一天，无实际意义。
            String panduanDate = "YYYY-MM-DD";
            for (JSONObject jsonObject : faceDistance) {
                jsonObject.put("distance", "");
                if (!StringUtils.isBlank(jsonObject.getString("regionLatitude")) && !StringUtils.isBlank(jsonObject.getString("regionLongitude"))
                        && !StringUtils.isBlank(jsonObject.getString("bigRegionLatitude")) && !StringUtils.isBlank(jsonObject.getString("bigRegionLongitude"))) {
                    double lat1 = Double.parseDouble(jsonObject.getString("regionLatitude"));
                    double lng1 = Double.parseDouble(jsonObject.getString("regionLongitude"));
                    double lat2 = Double.parseDouble(jsonObject.getString("bigRegionLatitude"));
                    double lng2 = Double.parseDouble(jsonObject.getString("bigRegionLongitude"));
                    String distance = common.getDistanceByLatAndLng(lat1, lng1, lat2, lng2);
                    if (distance.indexOf("公里") != -1) {
                        distance = new BigDecimal(distance.substring(0, distance.indexOf("公里"))).toString();
                    } else {
                        distance = (new BigDecimal(distance.substring(0, distance.indexOf("米")))).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP).toString();
                    }
                    jsonObject.put("distance", distance);
                }
                jsonObject.remove("regionLatitude");
                jsonObject.remove("regionLongitude");
                jsonObject.remove("bigRegionLatitude");
                jsonObject.remove("bigRegionLongitude");
            }
            result.put("faceTargetInfo", faceInfo);
            result.put("faceTargetDay", faceDistance);
        } else {
            telDistance = visitCustomerDao.getPeopleByUserId(userId, time, todayTime, common.NUMBER_ZERO, spacing);
            telInfo = visitCustomerDao.getFaceTargetAndComplete(userId, dateTime, spacing, todayTime, visitType, yesdayTime);
            result.put("telTargetDay", telDistance);
            result.put("telTargeInfo", telInfo);
        }
        return result;
    }

}
